package com.framework;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.framework.jqgrid.JsonDateValueProcessor;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class ActionUtil {
    public ActionUtil() {
    }

    private static final Log log = LogFactory.getLog(ActionUtil.class);

    public static int getIndex(java.lang.String as[], java.lang.String s) {
        if (as != null) {
            for (int i = 0; i < as.length; i++)
                if (s.equalsIgnoreCase(as[i]))
                    return i;

            return -1;
        } else {
            return -1;
        }
    }

    public static void handleProxyRequest(
            javax.servlet.http.HttpServletResponse httpservletresponse,
            java.lang.String s) throws java.lang.Exception {
        java.lang.StringBuffer stringbuffer = new StringBuffer("#");
        //stringbuffer.append(com.tzsw.plat.commons.StringUtil.ChineseStringToUTF(s));
        stringbuffer.append(s);
        stringbuffer.append("#");
        try {
            httpservletresponse.setContentType("text/xml;charset=utf-8");
            httpservletresponse.getWriter().println(stringbuffer.toString());
        } catch (java.io.IOException ioexception) {
        	log.error("", ioexception);
            httpservletresponse.getWriter().print("error!");
        }
    }

    public static void handleJqueryProxyRequest(
            javax.servlet.http.HttpServletResponse httpservletresponse,
            java.lang.String s) throws java.lang.Exception {
        java.lang.StringBuffer stringbuffer = new StringBuffer();
        //stringbuffer.append(com.tzsw.plat.commons.StringUtil.ChineseStringToUTF(s));
        stringbuffer.append(s);
        try {
            httpservletresponse.setContentType("text/html;charset=utf-8");
            httpservletresponse.getWriter().println(stringbuffer.toString());
        } catch (java.io.IOException ioexception) {
        	log.error("", ioexception);
            httpservletresponse.getWriter().print("error!");
        }
    }

    public static void ajaxProxyRequest(
            javax.servlet.http.HttpServletResponse response,
            String s) throws java.lang.Exception {
        response.setContentType("text/xml; charset=UTF-8");//传输xml时要用html
        response.setCharacterEncoding("UTF-8");
        PrintWriter pw = response.getWriter();
        pw.write(s);
    }

    /**
     * ajax成功返回datatype--默认为json
     *
     * @param response
     * @param info
     * @param data
     * @param datatype
     */
    public static void ajaxSuccess(
            javax.servlet.http.HttpServletResponse response,
            String info, String data, String datatype) {
        ajaxReturn(response, "1", info, data, datatype);
    }

    /**
     * ajax错误或者失败返回datatype--默认为json
     *
     * @param response
     * @param info
     * @param data
     * @param datatype
     */
    public static void ajaxFail(
            javax.servlet.http.HttpServletResponse response,
            String info, String data, String datatype) {
        ajaxReturn(response, "2", info, data, datatype);
    }

    /**
     * ajax成功，返回datatype为json
     *
     * @param response
     * @param info
     * @param data
     * @param datatype
     */
    public static void ajaxSuccess(
            javax.servlet.http.HttpServletResponse response,
            String info, String data) {
        ajaxReturn(response, "1", info, data, null);
    }

    /**
     * ajax错误，返回datatype为json
     *
     * @param response
     * @param info
     * @param data
     * @param datatype
     */
    public static void ajaxFail(
            javax.servlet.http.HttpServletResponse response,
            String info, String data) {
        ajaxReturn(response, "2", info, data, null);
    }

    public static void ajaxReturn(javax.servlet.http.HttpServletResponse response, String status, String info, String data, String contentType) {
        if (contentType != null && contentType.equals("html")) {
            log.info("contentType=" + contentType);
            response.setContentType("text/html; charset=UTF-8");//传输xml时要用html
        } else {
            log.info("contentType=text/json");
            response.setContentType("text/json; charset=UTF-8");//传输xml时要用html
        }

        response.setCharacterEncoding("UTF-8");
        try {
            PrintWriter pw;
            pw = response.getWriter();
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", status);
            map.put("info", info);
            map.put("data", data);
            pw.write(JSONObject.fromObject(map).toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
        	log.error("", e);
        }

    }

    public static void requestAppReturn(javax.servlet.http.HttpServletResponse response, Map<String, Object> retmap) {
        log.info("contentType=text/json");
        response.setContentType("text/json; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            PrintWriter pw;
            pw = response.getWriter();
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.registerJsonValueProcessor(java.util.Date.class,
                    new JsonDateValueProcessor());
            jsonConfig.registerJsonValueProcessor(java.sql.Timestamp.class,
                    new JsonDateValueProcessor());
            JSONObject json = JSONObject.fromObject(retmap, jsonConfig);
            pw.write(json.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
        	log.error("", e);
        }

    }

    public static void requestErrorReturn(javax.servlet.http.HttpServletResponse response, int code) {
        Map<String, Object> retmap = new HashMap<String, Object>();
        retmap.put("retcode", 1);
        retmap.put("error", code);
        requestAppReturn(response, retmap);

    }

    public static void requestSuccessReturn(javax.servlet.http.HttpServletResponse response, Map<String, Object> retmap) {
        retmap.put("retcode", 0);
        requestAppReturn(response, retmap);

    }
}
